package com.td.teach;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.td.teach.activity.Page1Activity;
import com.td.teach.activity.Page2Activity;
import com.td.teach.activity.Page3Activity;
import com.td.teach.activity.Page4Activity;
import com.td.teach.activity.Page5Activity;
import com.td.teach.activity.Page6Activity;
import com.td.teach.activity.Page7Activity;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    private List<View> mViews = new ArrayList<>();

    private ViewPager viewPager;

    private List<TextView> mTextViews = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);
        viewPager = (ViewPager) findViewById(R.id.vp_main);
        findViewById(R.id.img_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        initTextViews();

        initData();

        PagerAdapter adapter = new PagerAdapter() {
            @Override
            public Object instantiateItem(ViewGroup container, int position) {
                container.addView(mViews.get(position));

                return mViews.get(position);
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
                container.removeView(mViews.get(position));
            }

            @Override
            public boolean isViewFromObject(View view, Object object) {
                return view == object;
            }

            @Override
            public int getCount() {
                return mViews.size();
            }
        };

        viewPager.setAdapter(adapter);

        viewPager.setPageTransformer(true, new CustomTransformer());

        viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                toggleTextColor(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setOffscreenPageLimit(7);


    }

    private void initTextViews() {

        TextView tv1 = (TextView) findViewById(R.id.tv1);
        TextView tv2 = (TextView) findViewById(R.id.tv2);
        TextView tv3 = (TextView) findViewById(R.id.tv3);
        TextView tv4 = (TextView) findViewById(R.id.tv4);
        TextView tv5 = (TextView) findViewById(R.id.tv5);
        TextView tv6 = (TextView) findViewById(R.id.tv6);
        TextView tv7 = (TextView) findViewById(R.id.tv7);

        mTextViews.add(tv1);
        mTextViews.add(tv2);
        mTextViews.add(tv3);
        mTextViews.add(tv4);
        mTextViews.add(tv5);
        mTextViews.add(tv6);
        mTextViews.add(tv7);

        for (int i = 0; i < mTextViews.size(); i++) {
            mTextViews.get(i).setTag(i);
            mTextViews.get(i).setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    viewPager.setCurrentItem((Integer) v.getTag(),true);
                }
            });
        }

        tv1.setActivated(true);

    }

    public void toggleTextColor(int position) {
        for (TextView tv : mTextViews) {
            tv.setActivated(false);
        }
        mTextViews.get(position).setActivated(true);
    }

    private void initData() {

        View view1 = LayoutInflater.from(this).inflate(R.layout.activity_01, null);
        View view2 = LayoutInflater.from(this).inflate(R.layout.activity_02, null);
        View view3 = LayoutInflater.from(this).inflate(R.layout.activity_03, null);
        View view4 = LayoutInflater.from(this).inflate(R.layout.activity_04, null);
        View view5 = LayoutInflater.from(this).inflate(R.layout.activity_05, null);
        View view6 = LayoutInflater.from(this).inflate(R.layout.activity_06, null);
        View view7 = LayoutInflater.from(this).inflate(R.layout.activity_07, null);


        mViews.add(view1);
        mViews.add(view2);
        mViews.add(view3);
        mViews.add(view4);
        mViews.add(view5);
        mViews.add(view6);
        mViews.add(view7);

        view1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Page1Activity.class));
                overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }
        });
        view2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Page2Activity.class));
                overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }
        });
        view3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Page3Activity.class));
                overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }
        });
        view4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, ContentActivity.class));
                overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }
        });
        view5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Page5Activity.class));
                overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }
        });
        view6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Page6Activity.class));
                overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }
        });
        view7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MainActivity.this, Page7Activity.class));
                overridePendingTransition(R.anim.anim_enter, R.anim.anim_exit);
            }
        });

    }


}
